#include "LinkQueue.h"
QNode *BuyQNode(DataType val)
{
    QNode *newnode = (QNode *)malloc(sizeof(QNode));
    if (newnode == NULL)
    {
        perror("malloc fail");
        return NULL;
    }
    newnode->next_ = NULL;
    newnode->data_ = val;
    return newnode;
}
void InitQueue(LinkQueue *q)
{
    q->front_ = BuyQNode(-1);
    q->rear_ = q->front_;
}
void DestroyQueue(LinkQueue *q)
{
    QNode *tmp = q->front_->next_;
    while (tmp)
    {
        QNode *del = tmp->next_;
        free(tmp);
        tmp = del;
    }
    free(q->front_);
    q->front_ = q->rear_ = NULL;
}

bool Push(LinkQueue *q, DataType val)
{

    QNode *newnode = BuyQNode(val);
    q->rear_->next_ = newnode;
    q->rear_ = newnode;
    return true;
}
bool Pop(LinkQueue *q, DataType *val)
{
    if (Empty(q))
        return false;
    if (q->front_->next_ == q->rear_)
    {
        q->rear_ = q->front_;
    }
    QNode *del = q->front_->next_;
    q->front_->next_ = del->next_;
    *val = del->data_;
    free(del);
    return true;
}
bool Empty(LinkQueue *q)
{
    return q->front_->next_ == NULL;
}
bool Fill(LinkQueue *q)
{
    return false;
}
bool GetHead(LinkQueue *q, DataType *val)
{
    if (Empty(q))
    {
        *val = -1;
        return false;
    }
    *val = q->front_->next_->data_;
    return true;
}
int Length(LinkQueue *q)
{
    int length = 0;
    QNode *cur = q->front_;
    while (cur->next_)
    {
        length++;
        cur = cur->next_;
    }
    return length;
}
